package day8;

import org.junit.Test;


import java.io.*;

public class IODemo {
    @Test
    public void test1() {
        try {
            FileReader fileReader = new FileReader("a.txt");
            int ch1 = fileReader.read();
            System.out.println((char)ch1);//a
            int ch2 = fileReader.read();
            System.out.println((char)ch2);//b
            int ch3 = fileReader.read();
            System.out.println((char)ch3);//c
            int ch4 = fileReader.read();
            System.out.println(ch4);//-1
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test2() {
        try {
            FileReader fileReader = new FileReader("b.txt");
            int ch = -1;
            while ((ch = fileReader.read()) != -1) {
                System.out.println((char)ch);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Test
    public void test3() {
        FileReader fileReader = null;
        FileWriter fileWriter = null;

        try {
            fileReader = new FileReader("a.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            fileWriter = new FileWriter("b.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }

        char [] buffer = new char[10];
        int length = -1;
        while(true){
            try {
                while ((length = fileReader.read(buffer)) != -1){
                    System.out.println(buffer);
                    System.out.println(length);

                    fileWriter.write(buffer, 0, length);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(fileWriter != null){
                    try {
                        fileWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }


    }










    @Test
    public void testFileInputStreamFileOutputStram() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream("day11/img/1.png");
            fileOutputStream = new FileOutputStream("2.png");
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

